﻿using LogsClearWinService.Job;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using SYT.Common.QuartzUtil;
using SYT.Common.ServiceFactory;

namespace LogsClearWinService
{
    static class Program
    {
        public static Logger Loger = LogManager.GetLogger("LogsClearLog");

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
            //初始化
            Init();

            var baseWinServer = new BaseWinServer(Loger);
            baseWinServer.OnInit += BaseWinServer_OnInit;
            baseWinServer.OnStart += BaseWinServer_OnStart;
            baseWinServer.OnStop += BaseWinServer_OnStop;
            var winServiceFactory = new WinServiceFactory(Loger, baseWinServer);
            winServiceFactory.Build();
        }

        private static void Init()
        {
            string LogsClerCronExpress=System.Configuration.ConfigurationManager.AppSettings["LogsClerCronExpress"];
            QuartzHelper.CreateTaskJob<LogsClearJob>("LogsClearJob", "LogsClearJobGroup", "LogsClearJobTrigger", LogsClerCronExpress, null);
        }

        private static void BaseWinServer_OnStop(object sender, EventArgs e)
        {
            var serviceEventArgs = (ServiceEventArgs)e;
            serviceEventArgs._Logger.Info("清除日志服务已停止");
        }

        private static void BaseWinServer_OnStart(object sender, EventArgs e)
        {
            var serviceEventArgs = (ServiceEventArgs)e;
            serviceEventArgs._Logger.Info("清除日志服务已启动");
        }

        private static void BaseWinServer_OnInit(object sender, EventArgs e)
        {
            var serviceEventArgs = (ServiceEventArgs)e;
            serviceEventArgs._Logger.Info("清除日志服务初始化完成");
        }
    }
}
